using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Testing_ImportBundooraAgencies2007 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click1(object sender, EventArgs e)
    {
        DataSourceSelectArguments dssa = new DataSourceSelectArguments();

        dssa.AddSupportedCapabilities(DataSourceCapabilities.RetrieveTotalRowCount);
        dssa.RetrieveTotalRowCount = true;

        DataTable dt = ((DataView)this.SqlDataSource1.Select(dssa)).Table;

        PlacementManager.VenueComponent oVenueC = new PlacementManager.VenueComponent();
        PlacementManager.LinkVenueUnitComponent oLvuC = new PlacementManager.LinkVenueUnitComponent();
        PlacementManager.UnitComponent oUnitC = new PlacementManager.UnitComponent();

        StringBuilder sbMissingAgencies = new StringBuilder();
        int iAddedUnits = 0;
        //Let's do the agencies and their units
        foreach (DataRow dr in dt.Rows)
        {
            string agency = dr["Agency"].ToString();
            //try and find a match
            List<PlacementManager.Venue> oVenueList = oVenueC.SelectAllActiveStartingWith(agency, "VenueName");
            if (oVenueList.Count > 0)
            {
                //Get the venue object
                PlacementManager.Venue oVenue = oVenueList[0];
                

                string unitCode = (dr["Unit"].ToString()).Trim();

                int unitId = oUnitC.SelectOneWUnitCode(unitCode).UnitId;

                if (unitId > 0)
                {
                    PlacementManager.LinkVenueUnit oLvu = new PlacementManager.LinkVenueUnit();
                    oLvu.CreatedBy = "msmithers";
                    oLvu.DateCreated = DateTime.Now;
                    oLvu.DateUpdated = DateTime.Now;
                    oLvu.IsActive = true;
                    oLvu.UnitId = unitId;
                    oLvu.CampusId = 2;
                    oLvu.UpdatedBy = "msmithers";
                    oLvu.VenueId = oVenue.VenueId;
                    oLvuC.Insert(oLvu);

                    iAddedUnits++;
                }
            }
            else
            {
                sbMissingAgencies.Append("<br />" + agency);
            }
        }

        this.Label1.Text = "Units added:" + iAddedUnits.ToString() + " Missing Venues: " + sbMissingAgencies.ToString();
    }
}
